package com.im.controller;


import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.im.service.model.NoticeManageVo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.im.service.dto.Contract;
import com.im.service.dto.ContractClause;
import com.im.service.dto.ContractItem;
import com.im.service.dto.ImplementProject;
import com.im.service.dto.MeteDeclare;
import com.im.service.dto.PageQuery;
import com.im.service.dto.SearchOption;
import com.im.service.measureReport.smo.IMeasurementReportSMO;
import com.im.service.model.ContractItemVo;
import com.im.util.JsonUtil;
import com.im.util.ReturnsUtil;
import com.im.util.StringUtil;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/measureReportController")
public class MeasureReportController extends BaseController {

	private static final Logger logger = LogManager.getLogger(MeasureReportController.class);
	
	@Autowired
    private IMeasurementReportSMO measurementReportSMO;

    @RequestMapping("/measureReportManage")
    public String index() {
        return "measureReport/measureReport";
    }

    /**
     * 
     * 计量申报管理--列表查询. 
     * @param jsonStr
     * @return 
     */
	@RequestMapping("/queryContractItemList")
	@ResponseBody
	public String queryContractItemList(@RequestBody String jsonStr) {
		
		if (logger.isInfoEnabled()) {
			logger.info("请求入参json:{}", jsonStr);
		}
		try {
			if (jsonStr == null || jsonStr.length() <= 0) {
				return ReturnsUtil.returnFailure("请求入参为空，请核查数据!");
			}
			
			PageQuery<ContractItemVo> result = measurementReportSMO.queryContractItem(jsonStr);
			
			return ReturnsUtil.returnSuccess(result).toString();
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error(ReturnsUtil.getStackTrackMessage(e));
			}
			return ReturnsUtil.returnFailure(e.getMessage());
		}
    }
	
	/**
	 * 
	 * 计量申报--新增修改信息查询. 
	 * @param contract
	 * @return 
	 * @author xuwb
	 */
	@RequestMapping("/queryContractItemDetailByContractId")
	@ResponseBody
	public String queryContractItemDetailByContractId(@RequestBody Contract contract) {
		
		try {
			if (contract == null || contract.getContractId().length() <= 0) {
				return ReturnsUtil.returnFailure("请求入参为空，请核查数据!");
			}
			
			List<ContractItemVo> result = measurementReportSMO.queryContractItemDetailByContractId(contract);
			
			return ReturnsUtil.returnSuccess(result).toString();
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error(ReturnsUtil.getStackTrackMessage(e));
			}
			return ReturnsUtil.returnFailure(e.getMessage());
		}
    }
	
	
	/**
	 *
	 *  计量申报--新增修改信息. 
	 * @param implProj 入参
	 * @return
	 * @throws Exception
	 */
	@PostMapping(value = "/updateDeclarePay")
	@ResponseBody
	public String updateDeclarePay(@RequestBody ContractItemVo contractItem,HttpServletRequest request){
		if (logger.isInfoEnabled()) {
			logger.info("请求入参:{}", contractItem.toString());
		}
		try {
			if (contractItem == null) {
				ReturnsUtil.returnFailure("请求入参为空，请核查数据!");
			}
			measurementReportSMO.updateDeclarePay(contractItem,request);
			
			return ReturnsUtil.returnSuccess(new JSONObject()).toString();
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error(ReturnsUtil.getStackTrackMessage(e));
			}
			return ReturnsUtil.returnFailure(e.getMessage());
		}
	}

    @PostMapping("/queryImplProjectList")
    @ResponseBody
    public String queryImplProjectList() {
        List<ImplementProject> result = measurementReportSMO.queryImplProjectList();
        return ReturnsUtil.returnSuccess(result).toString();
    }


    @PostMapping("/queryContractListByImplProject")
    @ResponseBody
    public String queryContractListByImplProject(@RequestBody ImplementProject implementProject) {
        List<Contract> result = measurementReportSMO.queryContractListByImplProject(implementProject);
        return ReturnsUtil.returnSuccess(result).toString();
    }

    @PostMapping("/queryContractClauseListByContract")
    @ResponseBody
    public String queryContractClauseListByContract(@RequestBody Contract contract) {
        List<ContractClause> result = measurementReportSMO.queryContractClauseListByContract(contract);
        return ReturnsUtil.returnSuccess(result).toString();
    }

    @PostMapping("/queryContractItemListByContractClause")
    @ResponseBody
    public String queryContractItemListByContractClause(@RequestBody ContractClause contractClause) {
        List<ContractItem> result = measurementReportSMO.queryContractItemListByContractClause(contractClause);
        return ReturnsUtil.returnSuccess(result).toString();
    }

    @PostMapping("/queryContractItemInfo")
    @ResponseBody
    public String queryContractItemInfo(@RequestBody ContractItem contractItem) {
        List<ContractItem> result = measurementReportSMO.queryContractItemInfo(contractItem);
        return ReturnsUtil.returnSuccess(result).toString();
    }

    @PostMapping("/addMeasureReport")
    @ResponseBody
    public String addMeasureReport(@RequestBody MeteDeclare meteDeclare) {
        int count= measurementReportSMO.addMeasureReport(meteDeclare);
        return ReturnsUtil.returnSuccess(count).toString();
    }


    @PostMapping("/saveMeasureReport")
    @ResponseBody
    public String saveMeasureReport(@RequestBody MeteDeclare meteDeclare) {
        int count= measurementReportSMO.saveMeasureReport(meteDeclare);
        return ReturnsUtil.returnSuccess(count).toString();
    }

    @PostMapping("/delMeasureReport")
    @ResponseBody
    public String delMeasureReport(@RequestBody MeteDeclare meteDeclare) {
        int count= measurementReportSMO.delMeasureReport(meteDeclare);
        return ReturnsUtil.returnSuccess(count).toString();
    }

	@RequestMapping("/queryContractItemEaxmineList")
	@ResponseBody
	public String queryContractItemEaxmineList(@RequestBody String jsonStr) {
		ContractItemVo contractItemVo = JsonUtil.jsonToBean(jsonStr, ContractItemVo.class);
		SearchOption searchOption = JsonUtil.jsonToBean(jsonStr, SearchOption.class);
		PageQuery<ContractItemVo> result = measurementReportSMO.queryExamineContractItem(contractItemVo, searchOption);
		return ReturnsUtil.returnSuccess(result).toString();
	}
	@PostMapping("/examineMeasureReport")
	@ResponseBody
	public String examineMeasureReport(@RequestBody String jsonStr) {
		measurementReportSMO.examineMeasureReport(jsonStr);
		return ReturnsUtil.returnSuccess(new JSONObject()).toString();
	}

	@PostMapping("/examineMeasureReportByBatch")
	@ResponseBody
	public String examineMeasureReportByBatch(@RequestBody MeteDeclare meteDeclare) {
		measurementReportSMO.examineMeasureReportByBatch(meteDeclare);
		return ReturnsUtil.returnSuccess(new JSONObject()).toString();
	}



	/**
	 *
	 * 计量审核管理--列表查询.
	 * @param jsonStr
	 * @return
	 */
	@RequestMapping("/queryContractItemAuditList")
	@ResponseBody
	public String queryContractItemAuditList(@RequestBody String jsonStr) {

		if (logger.isInfoEnabled()) {
			logger.info("请求入参json:{}", jsonStr);
		}
		try {
			if (jsonStr == null || jsonStr.length() <= 0) {
				return ReturnsUtil.returnFailure("请求入参为空，请核查数据!");
			}

			PageQuery<ContractItemVo> result = measurementReportSMO.queryContractItemAuditList(jsonStr);

			return ReturnsUtil.returnSuccess(result).toString();
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error(ReturnsUtil.getStackTrackMessage(e));
			}
			return ReturnsUtil.returnFailure(e.getMessage());
		}
	}
}
